package com.rui.generator.service;

import com.rui.common.util.Pages;
import com.rui.generator.dao.GeneratorDao;
import com.rui.generator.entity.Column;
import com.rui.generator.entity.Generator;
import com.rui.generator.entity.Table;
import com.rui.generator.util.GeneratorUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipOutputStream;

/**
 * 代码生成 Service
 *
 * @author xr
 * @email xr@qq.com
 * @date 2020-01-01 00:00:00
 */
@Service
public class GeneratorService {

    @Resource
    private GeneratorDao dao;

    public Pages page(Map<String, Object> map) {
        return new Pages(dao.tableList(map));
    }

    /**
     * @description:
     * @param: [tableNames]
     * @return: byte[]
     * @author: xr
     * @date: 14:31 2020/1/10
     */
    public byte[] generatorCode(String[] tableNames) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(outputStream);
        for (String tableName : tableNames) {
            Table table = dao.getByTableName(tableName);
            List<Column> columnList = dao.columnList(tableName);
            GeneratorUtil.generator(new Generator(table, columnList), zip);
        }
        return outputStream.toByteArray();
    }


    public void generatorCode2(String[] tableNames) {
        ZipOutputStream zip = null;
        for(String tableName : tableNames){
//            //查询表信息
//            Map<String, String> table = queryTable(tableName);
//            //查询列信息
//            List<Map<String, String>> columns = queryColumns(tableName);
//            //生成代码
//            GeneratorUtil.generatorCode(table, columns, null, modelName);

            Table table = dao.getByTableName(tableName);
            List<Column> columnList = dao.columnList(tableName);
            GeneratorUtil.generator(new Generator(table, columnList), zip);
        }
    }

}
